package com.zhou.project.modules.circle.circleGroup.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.components.base.service.BaseService;
import com.zhou.project.modules.circle.circleGroup.dao.CircleGroupDao;
import com.zhou.project.modules.circle.circleGroup.entity.CircleGroup;
import com.zhou.project.modules.circle.circleType.dao.CircleTypeDao;
import com.zhou.project.modules.circle.circleType.entity.CircleType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@CacheConfig(cacheNames = "group")
public class CircleGroupService extends BaseService<CircleGroupDao, CircleGroup> {

    @Cacheable(key = "'page:'+#entity.size+':'+#entity.current")
    public Page<CircleGroup> page(CircleGroup entity)
    {
        //分页对象
        Page<CircleGroup> page=Page.of(entity.getCurrent(),entity.getSize());
        //按条件查询
        QueryWrapper<CircleGroup> wrapper=new QueryWrapper<>();
        if (StringUtils.hasLength(entity.getName()))
        {
            wrapper.like("name",entity.getName());
        }

        //调用分页查询
        return  baseMapper.selectPage(page,wrapper);

    }

    @Autowired
    private CircleTypeDao circleTypeDao;

    @Transactional
    public void insert(CircleGroup entity)
    {
        //保存圈子类型
        CircleType circleType=entity.getCircleType();
        circleTypeDao.insert(circleType);


        entity.setCircleId(circleType.getId());
        //自己的添加方法
        baseMapper.insert(entity);
    }

    //物理删除
    @Transactional
//    @CacheEvict(allEntries = true)  全清
//    @CacheEvict(condition = "id>18")  id>18才清
//    @CacheEvict(key="page:2:5")  只清空对应key
    @CacheEvict(allEntries = true)
    public int delete(int id)
    {
        return baseMapper.delete(id);
    }

    //查询组织中有多少人数

    public long CountPeople()
    {
        int userId=currentAuthentication.getCurrentUserId();

        return baseMapper.countPeople(userId);
    }

    //查询组织中有多少话题
    public long CountPosts(int id)
    {
        return baseMapper.countPosts(id);
    }


}
